/**
 * 温控计历史记录
 */
'use strict';

const conn = require('../../libs/DB/Conn');
const _ = require("underscore");
const decimal = require("decimal");
const {timeComponent,chartDataProcessing} = require("../../libs/Utils");
const columnNameObj = require("./deviceChartConfig").chart.temperature;
const deviceType = require("./deviceChartConfig").type.temperature;

module.exports = async function (params) {
    try{
    let timeInterval = timeComponent(params.startTime,params.endTime,params.type);
    let sql = `SELECT
        dd.device_no AS deviceNo,
        DATE_FORMAT(dd.data_time,"%Y-%m-%d %H:%i:%s") AS dataTime,
        ${timeInterval.queryGroupTime} 
        df.device_name AS deviceName,
        dd.temperature_mode AS temperatureMode,
        dd.preset_temperature AS presetTemperature,
        dd.indoor_temperature AS indoorTemperature,
        dd.airflow_mode AS airflowMode,
        dd.power_mode AS powerMode,
        dd.heating_high_setting_accumulated AS heatingHighSettingAccumulated,   
        dd.heating_high_setting_consumption AS heatingHighSettingConsumption,   
        dd.heating_medium_setting_accumulated AS heatingMediumSettingAccumulated,
        dd.heating_medium_setting_consumption AS heatingMediumSettingConsumption,
        dd.heating_low_setting_accumulated AS heatingLowSettingAccumulated,
        dd.heating_low_setting_consumption AS heatingLowSettingConsumption,
        dd.cooling_high_setting_accumulated AS coolingHighSettingAccumulated,   
        dd.cooling_high_setting_consumption AS coolingHighSettingConsumption,   
        dd.cooling_medium_setting_accumulated AS coolingMediumSettingAccumulated,
        dd.cooling_medium_setting_consumption AS coolingMediumSettingConsumption,
        dd.cooling_low_setting_accumulated AS coolingLowSettingAccumulated,
        dd.cooling_low_setting_consumption AS coolingLowSettingConsumption,
        rf.range_name AS rangeName
    FROM
    data_temperature_${params.userDomainId} dd
    INNER JOIN device_file df ON df.device_no = dd.device_no
    AND df.delete_sign = 1 AND df.device_type = ${deviceType} 
    AND df.range_id IN (?)
    INNER JOIN range_file rf ON df.range_id = rf.range_id AND rf.delete_sign = 1 
    WHERE 1 = 1 ${timeInterval.startTime && timeInterval.endTime ? ` AND (dd.data_time > "${timeInterval.startTime}" AND dd.data_time <= "${timeInterval.endTime}")` : `` } 
    ORDER BY dd.data_time DESC`;
    let temperatureDataList = await conn(sql,[params.rangeIds]);
    let temperatureDataGroup = _.groupBy(temperatureDataList,"deviceNo");
    let dataList = [];
    for(const deviceNo in temperatureDataGroup){
        let deviceDataList = temperatureDataGroup[deviceNo];
        let deviceObj = {
            deviceName:deviceDataList[0].deviceName,
            rangeName:deviceDataList[0].rangeName,
            deviceNo:deviceNo,
            presetTemperature:deviceDataList[0].presetTemperature,
            indoorTemperature:deviceDataList[0].indoorTemperature,
            airflowMode:deviceDataList[0].airflowMode,
            powerMode:deviceDataList[0].powerMode,
            heatingHighSettingAccumulated:deviceDataList[0].heatingHighSettingAccumulated,
            heatingHighSettingConsumption:0,
            heatingMediumSettingAccumulated:deviceDataList[0].heatingMediumSettingAccumulated,
            heatingMediumSettingConsumption:0,
            heatingLowSettingAccumulated:deviceDataList[0].heatingLowSettingAccumulated,
            heatingLowSettingConsumption:0,
            coolingHighSettingAccumulated:deviceDataList[0].coolingHighSettingAccumulated,
            coolingHighSettingConsumption:0,
            coolingMediumSettingAccumulated:deviceDataList[0].coolingMediumSettingAccumulated,
            coolingMediumSettingConsumption:0,
            coolingLowSettingAccumulated:deviceDataList[0].coolingLowSettingAccumulated,
            coolingLowSettingConsumption:0,
            dataList:[],
            chart:[]
        }
        deviceDataList.forEach(function (data){
            deviceObj.heatingHighSettingConsumption =  decimal.add(deviceObj.heatingHighSettingConsumption,data.heatingHighSettingConsumption).internal;
            deviceObj.heatingMediumSettingConsumption =  decimal.add(deviceObj.heatingMediumSettingConsumption,data.heatingMediumSettingConsumption).internal;
            deviceObj.heatingLowSettingConsumption =  decimal.add(deviceObj.heatingLowSettingConsumption,data.heatingLowSettingConsumption).internal;
            deviceObj.coolingHighSettingConsumption =  decimal.add(deviceObj.coolingHighSettingConsumption,data.coolingHighSettingConsumption).internal;
            deviceObj.coolingMediumSettingConsumption =  decimal.add(deviceObj.coolingMediumSettingConsumption,data.coolingMediumSettingConsumption).internal;
            deviceObj.coolingLowSettingConsumption =  decimal.add(deviceObj.coolingLowSettingConsumption,data.coolingLowSettingConsumption).internal;
        })
        if(params.type === "hour"){
            deviceObj.dataList = deviceDataList
        }else {
            let timeGroup = _.groupBy(deviceDataList,"groupTime");
            for(const time in timeGroup){
                let timeGroupList = timeGroup[time];
                let deviceDataObj = {
                    dataTime:time,
                    dataCreateTime:timeGroupList[timeGroupList.length - 1].dataTime,
                    presetTemperature:timeGroupList[0].presetTemperature,
                    indoorTemperature:timeGroupList[0].indoorTemperature,
                    airflowMode:timeGroupList[0].airflowMode,
                    powerMode:deviceDataList[0].powerMode,
                    heatingHighSettingAccumulated:timeGroupList[0].heatingHighSettingAccumulated,
                    heatingHighSettingConsumption:0,
                    heatingMediumSettingAccumulated:timeGroupList[0].heatingMediumSettingAccumulated,
                    heatingMediumSettingConsumption:0,
                    heatingLowSettingAccumulated:timeGroupList[0].heatingLowSettingAccumulated,
                    heatingLowSettingConsumption:0,
                    coolingHighSettingAccumulated:timeGroupList[0].coolingHighSettingAccumulated,
                    coolingHighSettingConsumption:0,
                    coolingMediumSettingAccumulated:timeGroupList[0].coolingMediumSettingAccumulated,
                    coolingMediumSettingConsumption:0,
                    coolingLowSettingAccumulated:timeGroupList[0].coolingLowSettingAccumulated,
                    coolingLowSettingConsumption:0,
                }
                deviceDataList.forEach(function (data){
                    deviceDataObj.heatingHighSettingConsumption =  decimal.add(deviceDataObj.heatingHighSettingConsumption,data.heatingHighSettingConsumption).internal;
                    deviceDataObj.heatingMediumSettingConsumption =  decimal.add(deviceDataObj.heatingMediumSettingConsumption,data.heatingMediumSettingConsumption).internal;
                    deviceDataObj.heatingLowSettingConsumption =  decimal.add(deviceDataObj.heatingLowSettingConsumption,data.heatingLowSettingConsumption).internal;
                    deviceDataObj.coolingHighSettingConsumption =  decimal.add(deviceDataObj.coolingHighSettingConsumption,data.coolingHighSettingConsumption).internal;
                    deviceDataObj.coolingMediumSettingConsumption =  decimal.add(deviceDataObj.coolingMediumSettingConsumption,data.coolingMediumSettingConsumption).internal;
                    deviceDataObj.coolingLowSettingConsumption =  decimal.add(deviceDataObj.coolingLowSettingConsumption,data.coolingLowSettingConsumption).internal;
                })
                deviceObj.dataList.push(deviceDataObj);
            }
        }
        deviceObj.chart = chartDataProcessing(deviceObj.dataList,columnNameObj);
        dataList.push(deviceObj);
    }
    return { success: true,dataList:dataList};
    }catch (e){
        return { success: false,dataList:[],message:e.code === "ER_NO_SUCH_TABLE" ? "当前账号无此类设备" : e.sqlMessage};
    }
};